// pages/tutorial/tutorial.js
Page({
  data: {
    htmlContent: ''
  },

  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '新手教程'
    })

    // 从全局或缓存中获取 study 内容
    const studyContent = wx.getStorageSync('studyContent') || ''
    
    if (studyContent) {
      this.setData({
        htmlContent: studyContent
      })
    } else {
      // 如果没有缓存，尝试从接口获取
      this.fetchStudyContent()
    }
  },

  fetchStudyContent() {
    const request = require('../../utils/request')
    
    wx.showLoading({ title: '加载中...' })
    
    request.get('/admin-api/mini/home/getRole')
      .then(res => {
        wx.hideLoading()
        if (res.code === 0 && res.data && res.data.study) {
          const studyContent = res.data.study
          wx.setStorageSync('studyContent', studyContent)
          this.setData({
            htmlContent: studyContent
          })
        } else {
          wx.showToast({ title: '暂无教程内容', icon: 'none' })
        }
      })
      .catch(err => {
        wx.hideLoading()
        console.error('获取教程内容失败:', err)
        wx.showToast({ title: '加载失败', icon: 'none' })
      })
  }
})
